﻿using Power.Models;
using Power.View.Report;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Power.ViewModel.Report
{
    /// <summary>
    /// 缺陷种类表
    /// </summary>
    public class DefectTypeTableViewModel : ReportTableBase
    {
        public List<DefectKindTableData> Data { get; set; }
        private DefectTypeTable model;
        public DefectTypeTableViewModel(DefectTypeTable model)
        {
            this.model = model;
            Data = CreateEntity(2, 5);

            Init();
        }

        /// <summary>
        /// 动态创建测试数据
        /// </summary>
        /// <param name="subCount"></param>
        /// <returns></returns>
        public List<DefectKindTableData> CreateEntity(int count,int subCount)
        {

            var list = new List<DefectKindTableData>();
            for (int i = 0; i < count; i++)
            {
                var obj = Activator.CreateInstance(typeof(DefectKindTableData));

                var type = obj.GetType();
                var props = type.GetProperties();
                foreach (var item in props)
                {
                    if (item.PropertyType == typeof(int))
                    {
                        item.SetValue(obj, 1);
                    }
                    else if (item.PropertyType == typeof(string))
                    {
                        item.SetValue(obj, "2");
                    }
                }
                var ret = obj as DefectKindTableData;
                ret.Value = new List<DefectKindBaseValue>();
                for (int j = 0; j < subCount; j++)
                {
                    ret.Value.Add(CreateSubEntity());
                }
                list.Add(ret);
            }
            

            
            return list;
        }
        /// <summary>
        /// 动态创建子项测试数据
        /// </summary>
        /// <returns></returns>
        public DefectKindBaseValue CreateSubEntity()
        {
            var obj = Activator.CreateInstance(typeof(DefectKindBaseValue));

            var type = obj.GetType();
            var props = type.GetProperties();
            foreach (var item in props)
            {
                if (item.PropertyType == typeof(int))
                {
                    item.SetValue(obj, 1);
                }
                else if (item.PropertyType == typeof(string))
                {
                    item.SetValue(obj, "2");
                }
            }

            return obj as DefectKindBaseValue;
        }
        public void Init()
        {
            //Row = Data.SelectMany(p => p.Value).Count() + Data.Count + 4;
            Row = Data.SelectMany(p => p.Value).Count()  + 3;
            Column = 8;
            var table = this.model.table;

            var titleList = new List<string>() { "缺陷类型", "缺陷名称", "1级", "2级", "3级", "4级", "合计", "备注" };

            for (int i = 0; i < Row; i++)
            {
                this.model.table.RowDefinitions.Add(new RowDefinition());

            }
            for (int i = 0; i < Column; i++)
            {
                this.model.table.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(150) });
            }

            //画标题
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < Column; j++)
                {
                    DrawColumnSpanCell(table, i, j, "缺陷种类表", Column, 0, 0);

                    DrawColumnSpanCell(table, i, j, "", 6, 1, 0);
                    DrawColumnSpanCell(table, i, j, "单位:处", 2, 1, 6);




                    DrawCell(table, i, j, "缺陷类型", 2, 0);
                    DrawCell(table, i, j, "缺陷名称", 2, 1);
                    DrawCell(table, i, j, "1级", 2, 2);
                    DrawCell(table, i, j, "2级", 2, 3);
                    DrawCell(table, i, j, "3级", 2, 4);
                    DrawCell(table, i, j, "4级", 2, 5);
                    DrawCell(table, i, j, "合计", 2, 6);
                    DrawCell(table, i, j, "备注", 2, 7);

                }
            }
            
            var firstRow = 3;
            var currentRow = 3;
            for (int i = 0; i < Data.Count; i++)
            {
                var item = Data[i];
                for (int j = 0; j < item.Value.Count; j++)
                {
                    var row = item.Value[j];
                    var columnIndex = 0;
                    DrawRowSpanCell(this.model.table, currentRow, columnIndex, item.DefectType, item.Value.Count, firstRow, columnIndex++);
                    DrawCell(this.model.table, currentRow, columnIndex, row.DefectName, currentRow, columnIndex++);
                    DrawCell(this.model.table, currentRow, columnIndex, row.Lv1.ToString(),currentRow,columnIndex++);
                    DrawCell(this.model.table, currentRow, columnIndex, row.Lv2.ToString(), currentRow, columnIndex++);
                    DrawCell(this.model.table, currentRow, columnIndex, row.Lv3.ToString(), currentRow, columnIndex++);
                    DrawCell(this.model.table, currentRow, columnIndex, row.Lv4.ToString(), currentRow, columnIndex++);
                    DrawCell(this.model.table, currentRow, columnIndex, row.Sum.ToString(), currentRow, columnIndex++);
                    DrawCell(this.model.table, currentRow, columnIndex, row.Remark, currentRow, columnIndex++);

                    currentRow++;
                }

                //小计
                //{
                //    var columnIndex = 0;
                //    DrawRowSpanCell(this.model.table, currentRow, columnIndex, item.DefectType, item.Value.Count + 1, firstRow, columnIndex++);
                //    DrawCell(this.model.table, currentRow, columnIndex, "小计", currentRow, columnIndex++);
                //    DrawCell(this.model.table, currentRow, columnIndex, item.Value.Sum(p => p.Lv1).ToString(), currentRow, columnIndex++);
                //    DrawCell(this.model.table, currentRow, columnIndex, item.Value.Sum(p => p.Lv2).ToString(), currentRow, columnIndex++);
                //    DrawCell(this.model.table, currentRow, columnIndex, item.Value.Sum(p => p.Lv3).ToString(), currentRow, columnIndex++);
                //    DrawCell(this.model.table, currentRow, columnIndex, item.Value.Sum(p => p.Lv4).ToString(), currentRow, columnIndex++);
                //    DrawCell(this.model.table, currentRow, columnIndex, item.Value.Sum(p => p.Lv1 + p.Lv2 + p.Lv3 + p.Lv4).ToString(), currentRow, columnIndex++);
                //    DrawCell(this.model.table, currentRow, columnIndex, "", currentRow, columnIndex++);

                //    currentRow++;
                //}
                firstRow = currentRow;
            }

            //合计
            //{
            //    var columnIndex = 0;
            //    DrawCell(this.model.table, currentRow, columnIndex, "合计", currentRow, columnIndex++);
            //    DrawCell(this.model.table, currentRow, columnIndex, "-", currentRow, columnIndex++);
            //    DrawCell(this.model.table, currentRow, columnIndex, Data.SelectMany(p => p.Value).Sum(p => p.Lv1).ToString(), currentRow, columnIndex++);
            //    DrawCell(this.model.table, currentRow, columnIndex, Data.SelectMany(p => p.Value).Sum(p => p.Lv2).ToString(), currentRow, columnIndex++);
            //    DrawCell(this.model.table, currentRow, columnIndex, Data.SelectMany(p => p.Value).Sum(p => p.Lv3).ToString(), currentRow, columnIndex++);
            //    DrawCell(this.model.table, currentRow, columnIndex, Data.SelectMany(p => p.Value).Sum(p => p.Lv4).ToString(), currentRow, columnIndex++);
            //    DrawCell(this.model.table, currentRow, columnIndex, Data.SelectMany(p => p.Value).Sum(p => p.Lv1 + p.Lv2 + p.Lv3 + p.Lv4).ToString(), currentRow, columnIndex++);
            //    DrawCell(this.model.table, currentRow, columnIndex, "", currentRow, columnIndex++);

            //    currentRow++;
            //}

            //补边框
            DrawBorder(table);
        }
    }
}
